package com.eveinfo;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.eveinfo.api.AbstractApiParser;
import com.eveinfo.sql.PictureBaseHelper;
import com.eveinfo.tools.ConfigFileException;
import com.eveinfo.tools.EveInfoMenu;
import com.eveinfo.tools.EveInfoPreferences;
import com.eveinfo.tools.Logging;
import com.eveinfo.tools.Pilot;
import com.eveinfo.tools.Setting;
import com.eveinfo.update.EveInfoData;
import com.eveinfo.update.UpdateService;

public class EveInfo extends Activity {

	public static boolean DELETE_CACHE = false;

	public static Activity activity;

	private static EveInfoData data;

	private static ArrayList<Pilot> pilots;
	private static PictureBaseHelper pictureBaseHelper;
	private static EveInfoPreferences preferences;

	public EveInfo() {
		activity = this;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.eveinfo);

		getDir("cache", 0);
		getDir("planner", 0);
		
		data = new EveInfoData(this);
		Intent svc = new Intent(this, UpdateService.class);
		startService(svc);

		pictureBaseHelper = new PictureBaseHelper(this);

		try {
			InetAddress.getByName(AbstractApiParser.EVE_API_SERVER);

			
			try {
				pilots = Setting.readOptions(this);
			} catch (ConfigFileException e) {
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle("Configuration File Error");
				builder.setMessage(e.getMessage());
				builder.show();
				e.printStackTrace();
			}
			Button skills = (Button) findViewById(R.id.BEveInfoSkills);
			skills.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					EveInfoMenu.displayView(activity, EveInfoMenu.View.SKILLS);
				}
			});

			Button characters = (Button) findViewById(R.id.BEveInfoCharacters);
			characters.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					EveInfoMenu.displayView(activity, EveInfoMenu.View.PILOTS);
				}
			});

			Button options = (Button) findViewById(R.id.BEveInfoOptions);
			options.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					EveInfoMenu.displayView(activity, EveInfoMenu.View.OPTIONS);
				}
			});
		} catch (UnknownHostException e) {
			Logging.getInstance().e(e);
			e.printStackTrace();
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Api Server Down");
			builder.setMessage("The EveOnline Api server is down.");
			builder.show();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Logging.getInstance().close();
	}

	public static ArrayList<Pilot> getPilots() {
		if (pilots == null)
			pilots = new ArrayList<Pilot>();
		return pilots;
	}

	public static void setPilots(ArrayList<Pilot> pilots) {
		EveInfo.pilots = null;
		EveInfo.pilots = pilots;
	}

	public static PictureBaseHelper getPictureBaseHelper() {
		return pictureBaseHelper;
	}

	public static EveInfoPreferences getPreferences() {
		if (preferences == null) {
			preferences = new EveInfoPreferences(activity);
		}
		return preferences;
	}

	public void createPictureDB() {
		String dirName = "/sdcard/eveinfo/pics/";
		File dir = new File(dirName);
		String[] list = dir.list();

		PictureBaseHelper pictureBaseHelper = new PictureBaseHelper(this);
		pictureBaseHelper.openDataBase();
		for (String fileName : list) {
			String name = fileName.substring(0, fileName.length() - 4);
			File file = new File(dirName + fileName);
			System.out.println(file.getName());
			int length = (int) file.length();
			byte[] data = new byte[length];
			try {
				BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
				in.read(data, 0, length);
			} catch (IOException e) {
				Logging.getInstance().e(e);
				e.printStackTrace();
			}
			pictureBaseHelper.insertPicture(name, data);
		}
		pictureBaseHelper.getPicture("test");
		pictureBaseHelper.close();
	}

	public static EveInfoData getData() {
		if(data == null){
			data = new EveInfoData(activity);
		}
		return data;
	}
}
